package com.ara.common.kernel.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.ara.common.kernel.network.exception.ExceptionHandle;
import com.ara.common.kernel.util.ToastUtils;

/**
 * Created by XieXin on 2017-09-15.
 * MVP BaseFragment
 */

public abstract class BaseMvpFragment<VB extends ViewBinding, V extends BaseView, T extends BasePresenter<V>>
        extends BaseFragment<VB> {
    protected T presenter;

    protected abstract T createPresenter();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        presenter = createPresenter();
        getLifecycle().addObserver(presenter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * Toast
     *
     * @param e
     */
    protected void onBaseToast(ExceptionHandle.ResponseThrowable e) {
        if (e.getMsgResId() == 0) {
            ToastUtils.showLong(getContext(), e.getMsg());
        } else {
            ToastUtils.showLong(getContext(), e.getMsgResId());
        }
    }
}
